package au.com.handsunited.web.controller.user;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

import au.com.handsunited.model.BaseEntity;
import au.com.handsunited.model.User;
import au.com.handsunited.service.EntityService;
import au.com.handsunited.service.UserService;

/**
 * @author arunda
 *
 */
public class UserListController extends AbstractController {

	private EntityService entityService;
	private UserService userService;
	private String successView;


	private static Logger logger = LoggerFactory.getLogger(UserListController.class);


	/**
	 *
	 */
	public UserListController() {
		setSupportedMethods(new String[]{"GET"});
	}

	/**
	 * @see org.springframework.web.servlet.mvc.AbstractController#handleRequestInternal(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	@Override
	protected ModelAndView handleRequestInternal(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		String id = request.getParameter("id");
		List<User> users;

		ModelAndView mav = new ModelAndView(successView);
		if (StringUtils.isNotEmpty(id)) {
			BaseEntity entity = entityService.getEntity(id);
			if (entity != null) {
				users = entity.getUsersInNetwork();
			} else {
				users = userService.getAllUsers();
			}
		} else {
			users = userService.getAllUsers();
		}

		mav.addObject("users", users);
		return mav;
	}

	/**
	 * @param entityService the entityService to set
	 */
	public void setUserService(EntityService entityService) {
		this.entityService = entityService;
	}

	/**
	 * @param entityService the entityService to set
	 */
	public void setEntityService(EntityService entityService) {
		this.entityService = entityService;
	}

	/**
	 * @param successView the successView to set
	 */
	public void setSuccessView(String successView) {
		this.successView = successView;
	}

}
